package cn.xfdzx.android.apps.shop.activity.procurement;


import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.ProcurementTypeListAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.ProcurementTypeListBeen;
import cn.xfdzx.android.apps.shop.bean.ProcurementTypeSubmitBeen;
import cn.xfdzx.android.apps.shop.bean.PurchaseSubmitBeen;
import cn.xfdzx.android.apps.shop.contract.ProcurementTypeListContract;
import cn.xfdzx.android.apps.shop.evenbus.PFCaiSelectNumMessage;
import cn.xfdzx.android.apps.shop.presenter.ProcurementTypeListPresenter;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.Utils;

//批发分类
public class ProcurementTypeListActivity extends BaseMvpActivity<ProcurementTypeListPresenter> implements ProcurementTypeListContract.View {

    @BindView(R.id.rv_procurement_type_list)
    RecyclerView rvProcurementTypeList;
    @BindView(R.id.tv_total)
    TextView tvTotal;

    private ProcurementTypeListAdapter procurementTypeListAdapter;
    private List<ProcurementTypeListBeen.DataBean> mList = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_procurement_type_list;
    }

    @Override
    public void initView() {
        mPresenter = new ProcurementTypeListPresenter();
        mPresenter.attachView(this);
        EventBus.getDefault().register(this);

        procurementTypeListAdapter = new ProcurementTypeListAdapter(this, mList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvProcurementTypeList.setLayoutManager(linearLayoutManager);
        rvProcurementTypeList.setItemAnimator(new DefaultItemAnimator());
        rvProcurementTypeList.setNestedScrollingEnabled(false);
        rvProcurementTypeList.setAdapter(procurementTypeListAdapter);


        mPresenter.procurementTypeList();
    }

    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(this);
        }

    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {

    }


    @Subscribe(threadMode = ThreadMode.POSTING)
    public void XXX(PFCaiSelectNumMessage messageEvent) {
        int total_type_num = 0;
        int total_jian_num = 0;
        for (ProcurementTypeListBeen.DataBean dataBean : mList) {
            int select_type_num = 0;
            int jian_num = 0;
            for (ProcurementTypeListBeen.DataBean.GoodsListBean goodListbeen : dataBean.getGoods_list()) {
                if (goodListbeen.isIs_select()) {
                    select_type_num++;
                    jian_num += goodListbeen.getGoods_num();
                }
            }
            total_type_num += select_type_num;
            total_jian_num += jian_num;
        }
        tvTotal.setText("共" + total_type_num + "种，" + total_jian_num + "件。");
    }

    @Override
    public void onProcurementTypeListSuccess(ProcurementTypeListBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            mList.clear();
            mList.addAll(bean.getData());
            ProcurementTypeListBeen.DataBean dataBean = new ProcurementTypeListBeen.DataBean();
            dataBean.setCategory_id("0");
            dataBean.setCategory_name("自定义");
            List<ProcurementTypeListBeen.DataBean.GoodsListBean> beanList = new ArrayList<>();
            dataBean.setGoods_list(beanList);


            for (int k = 0; k < 6; k++) {
                ProcurementTypeListBeen.DataBean.GoodsListBean goodsListBean = new ProcurementTypeListBeen.DataBean.GoodsListBean();
                goodsListBean.setGoods_spec("斤");
                goodsListBean.setGoods_producer("全国");
                goodsListBean.setPurchase_type(2);
                beanList.add(goodsListBean);
            }
            mList.add(dataBean);
            procurementTypeListAdapter.notifyDataSetChanged();
        } else {
            Utils.toastMessage(this, bean.getMsg());
        }

    }

    @Override
    public void onPurchaseSubmitSuccess(PurchaseSubmitBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            Intent intent = new Intent(this, ProcurementConfimPayActivity.class);
            intent.putExtra("data", bean.getData());
            startActivity(intent);
            this.finish();
        } else {
            Utils.toastMessage(this, bean.getMsg());
        }
    }

    @Override
    protected void onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
    }

    @OnClick({R.id.iv_back, R.id.tv_go_dinggou})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:    //返回按钮点击
                finish();
                break;
            case R.id.tv_go_dinggou:    //
                ProcurementTypeSubmitBeen pbeen = new ProcurementTypeSubmitBeen();
                for (ProcurementTypeListBeen.DataBean dataBean : mList) {
                    for (ProcurementTypeListBeen.DataBean.GoodsListBean goodListbeen : dataBean.getGoods_list()) {
                        if (goodListbeen.isIs_select()) {
                            pbeen.getGoods_list().add(goodListbeen);
                        }
                    }
                }
                if (pbeen.getGoods_list().size() == 0) {
                    Utils.toastMessage(this, "请选择品种数量");
                    return;
                }
                mPresenter.purchaseSubmit(pbeen);

                break;

        }
    }
}
